package com.qujia.station.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qujia.common.exception.BaseException;
import com.qujia.station.domain.Enterprise;
import com.qujia.station.domain.Oil;
import com.qujia.station.domain.OilOmeter;
import com.qujia.station.domain.Station;
import com.qujia.station.mapper.OilOmeterMapper;
import com.qujia.station.service.IEnterpriseService;
import com.qujia.station.service.IOilService;
import com.qujia.station.service.IStationService;
import com.qujia.station.service.OilOmeterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service("oilOmeterService")
@Slf4j
public class OilOmeterServiceImpl implements OilOmeterService {

    @Resource
    private OilOmeterMapper oilOmeterMapper;

    @Resource
    private IEnterpriseService enterpriseService;

    @Autowired
    private IStationService stationService;

    @Resource
    private IOilService oilService;

    @Override
    public int saveOmter(OilOmeter oilOmeter) {
        if(Objects.isNull(oilOmeter)){
            throw new BaseException("请求参数异常，新增油罐失败");
        }
        log.info("saveOmter 保存油罐数据：[{}]", JSONObject.toJSONString(oilOmeter));
        Station station = stationService.selectStationById(oilOmeter.getStationId());
        if(Objects.isNull(oilOmeter.getEnterpriseId())){
            oilOmeter.setEnterpriseId(station.getEnterpriseId());
        }

        Enterprise enterprise = enterpriseService.selectEnterpriseById(oilOmeter.getEnterpriseId());
        if(Objects.nonNull(enterprise)){
            oilOmeter.setEnterpriseName(enterprise.getEnterpriseName());
        }

        if(Objects.nonNull(station)){
            oilOmeter.setStationName(station.getStationName());
        }

        Oil oil = oilService.selectOilById(oilOmeter.getOilId());
        if(Objects.nonNull(oil)){
            oilOmeter.setOilName(oil.getOilName());
        }

        return oilOmeterMapper.insertSelective(oilOmeter);
    }

    @Override
    public int updateOmter(OilOmeter oilOmeter) {
        if(Objects.isNull(oilOmeter)){
            throw new BaseException("请求参数异常，编辑油罐失败");
        }
        if(Objects.nonNull(oilOmeter.getOilId())){
            Oil oil = oilService.selectOilById(oilOmeter.getOilId());
            if(Objects.isNull(oil)){
                throw new BaseException("请求参数异常，油品不存在");
            }
            oilOmeter.setOilName(oil.getOilName());
        }
        log.info("updateOmter 保存油罐数据：[{}]", JSONObject.toJSONString(oilOmeter));
        oilOmeter.setUpdateTime(new Date());
        return oilOmeterMapper.updateByPrimaryKey(oilOmeter);
    }

    @Override
    public List<OilOmeter> selectOmeterList(OilOmeter oilOmeter) {
        log.info("updateOmter 保存油罐数据：[{}]", JSONObject.toJSONString(oilOmeter));
        List<OilOmeter> oilOmeterList = oilOmeterMapper.selectOilOmeterList(oilOmeter);
        return oilOmeterList;
    }

    @Override
    public int deleteOmeter(Long ometerId) {
        return oilOmeterMapper.updateOilOmeter(ometerId);
    }
}
